<template>
    <div class="app-container">
        <el-card v-show="showSearch">
            <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="" size="small">
                <el-form-item label="设备地址" prop="peerAddress">
                    <el-input v-model="queryParams.peerAddress" clearable placeholder="请输入设备地址" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="oid" prop="oid">
                    <el-input v-model="queryParams.oid" clearable placeholder="请输入oid" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="main-card" shadow="never">
            <el-row :gutter="10" class="mb20">
                <div class="fr">
                    <el-tooltip class="item" effect="dark" content="导出" placement="top">
                        <el-button size="mini" style="margin-right: 10px" circle icon="el-icon-download" v-hasPermi="['data:response:export']" @click="handleExport" />
                    </el-tooltip>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </div>
            </el-row>
            <el-table class="base-table" :border="false" v-loading="loading" :data="responseList" @selection-change="handleSelectionChange">
                <el-table-column align="center" type="selection" width="55" />
                <el-table-column align="center" label="设备地址" prop="peerAddress" />
                <el-table-column align="center" label="oid" prop="oid" />
                <el-table-column align="center" label="响应的值" prop="variable" width="600" />
                <el-table-column align="center" label="响应时间" prop="createTime" />
                <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
                    <template slot-scope="scope">
                        <el-button v-hasPermi="['data:response:show']" icon="el-icon-show" size="mini" type="text" @click="handleShow(scope.row)">查看</el-button>
                        <!--                    <el-button
                                  size="mini"
                                  type="text"
                                  icon="el-icon-edit"
                                  @click="handleUpdate(scope.row)"
                                  v-hasPermi="['data:response:edit']"
                              >修改</el-button>
                              <el-button
                                  size="mini"
                                  type="text"
                                  icon="el-icon-delete"
                                  @click="handleDelete(scope.row)"
                                  v-hasPermi="['data:response:remove']"
                              >删除</el-button>-->
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList" />
        </el-card>
        <!-- 添加或修改snmp响应结果对话框 -->
        <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="响应的值" prop="variable">
                    <el-input v-model="form.variable" placeholder="请输入响应的值" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 查看snmp响应结果对话框 -->
        <el-dialog :title="title" :visible.sync="show" append-to-body width="800px">
            <el-form ref="form" :model="form" :rules="rules" disabled label-width="80px">
                <el-form-item label="设备地址" prop="peerAddress">
                    <el-input v-model="form.peerAddress" />
                </el-form-item>
                <el-form-item label="oid" prop="oid">
                    <el-input v-model="form.oid" />
                </el-form-item>
                <el-form-item label="响应的值" prop="variable">
                    <el-input v-model="form.variable" />
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import { listResponse, getResponse, delResponse, addResponse, updateResponse } from '@/api/snmp/snmpResponse';

export default {
    name: 'Response',
    data() {
        return {
            show: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // snmp响应结果表格数据
            responseList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                oid: null,
                peerAddress: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询snmp响应结果列表 */
        getList() {
            this.loading = true;
            listResponse(this.queryParams).then((response) => {
                this.responseList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                oid: null,
                variable: null,
                peerAddress: null,
                createTime: null,
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = '添加snmp响应结果';
        },
        handleShow(row) {
            this.reset();
            const id = row.id || this.ids;
            getResponse(id).then((response) => {
                this.form = response.data;
                this.show = true;
                this.title = '查看snmp响应结果';
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getResponse(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = '修改snmp响应结果';
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateResponse(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功');
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addResponse(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功');
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除snmp响应结果编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delResponse(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'data/response/export',
                {
                    ...this.queryParams,
                },
                `response_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
